/**
 * e3db Fast database library for Grails
 * Copyright (C) 2009-2010 Collegeman.net, LLC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package net.collegeman.grails.e3db;

import java.util.*;
import javax.sql.*;
import groovy.lang.*;
import org.springframework.jdbc.support.rowset.*;

public class DB extends Template {
	
	public static List<Map<String, Object>> list(String sql, Object ... args) {
		return query(METHOD_LIST, sql, List.class, args);
	}
	
	public static List<Map<String, Object>> list(Closure closure) {
		return query(METHOD_LIST, closure, List.class);
	}
	
	public static GroovySqlRowSet rows(String sql, Object ... args) {
		return new GroovySqlRowSet(rowSet(sql, args));
	}
	
	public static SqlRowSet rowSet(String sql, Object ... args) {
		return query(METHOD_ROW_SET, sql, SqlRowSet.class, args);
	}	
	
	public static GroovySqlRowSet rows(Closure closure) {
		return new GroovySqlRowSet(rowSet(closure));
	}
	
	public static SqlRowSet rowSet(Closure closure) {
		return query(METHOD_ROW_SET, closure, SqlRowSet.class);
	}
	
	public static Object get(String sql, Object ... args) {
		return object(sql, Object.class, args);
	}
	
	public static Object get(Closure closure) {
		return object(closure, Object.class);
	}
	
	public static Integer getInt(String sql, Object ... args) {
		return object(sql, Integer.class, args);
	}
	
	public static Integer getInt(Closure closure) {
		return object(closure, Integer.class);
	}
	
	public static Long getLong(String sql, Object ... args) {
		return object(sql, Long.class, args);
	}
	
	public static Long getLong(Closure closure) {
		return object(closure, Long.class);
	}
	
	public static Object object(String sql, Object ... args) {
		return query(METHOD_OBJECT, sql, Object.class, args);
	}
	
	public static <T> T object(String sql, Class<T> requiredType, Object ... args) {
		return (T) query(METHOD_OBJECT, sql, requiredType, args);
	}
	
	public static <T> T object(Closure closure, Class<T> requiredType) {
		return (T) query(METHOD_OBJECT, closure, requiredType);
	}
	
	public static int update(String sql, Object ... args) {
		return exec(sql, args);
	}
	
	public static int update(Closure closure) {
		return exec(closure);
	}
	
	public static int[] batch(String sql, List<List<Object>> batchArgs) {
		List<Object[]> batches = new ArrayList<Object[]>();
		for(List<Object> batch : batchArgs) {
			batches.add(batch.toArray());
		}
		return new Template(){}.getSJT().batchUpdate(sql, batches);
	}
	
}